#!/usr/bin/python
# Filename: mjs_startup.py

import sys
import logging
import time

from mjs_conf import mjs_conf
from mjc_server import mjc_server
from mjc_server import mjc_flash_status
from mjw_server import mjw_server
from file_server import file_server

config_file = "/etc/mangjings/conf/mangjings.json"

def log_conf(is_debug):
    if is_debug:
        logging.basicConfig(level=logging.DEBUG,
            format='%(asctime)s %(filename)s+%(lineno)d [%(levelname)s] %(message)s',
            filemode='w')
    else:
        logging.basicConfig(level=logging.INFO,
            format='%(asctime)s %(filename)s+%(lineno)d [%(levelname)s] %(message)s',
            filename='/etc/mangjings/logs/mangjings.log',
            filemode='w')
    pass

def main():
    is_debug = False
    # is_debug = True
    if len(sys.argv) > 1 and sys.argv[1] == "debug":
        is_debug = True

    log_conf(is_debug)

    if is_debug:
        logging.info("mjs debug mode running")

    mjs_conf_mgr = mjs_conf.MjsConf()
    if not mjs_conf_mgr.load(config_file):
        logging.error("read conifg failed")
        return

    mjw_server_obj = mjw_server.MjwServer()
    if not mjw_server_obj.startup():
        logging.error('mjw server startup failed')
        exit()

    mjc_flash = mjc_flash_status.MjcFlashStatus()
    if not mjc_flash.startup():
        logging.error('mjc flash startup failed')
        exit()

    mjc_server_obj = mjc_server.MjcServer(mjw_server_obj)
    if not mjc_server_obj.startup():
        logging.error('mjc server startup failed')
        exit()

    file_server_obj = file_server.FileServer(mjc_server_obj)
    if not file_server_obj.startup():
        logging.error('file server startup failed')
        exit()

    while True:
        #logging.debug('mjs running...')
        time.sleep(10)

    pass

if __name__ == "__main__":
    main()
else:
    pass
